
/**   
 * @Title: PreferencesWrapper.java 
 * @Package: com.rename.icode.utils 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-2-11 下午5:23:47 
 * @version  
 */


package com.rename.icode.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.rename.icode.comm.Constants;

import java.util.HashMap;

/** 
 * @Description 实现了...功能
 * @author Administrator
 * @date 2015-2-11 下午5:23:47 
 * @version V1.0
 */

public class PreferencesWrapper {
	private static final String TAG = "PreferencesWrapper";
	private SharedPreferences prefs;
	private Context context;
	private Editor sharedEditor;
	
	public PreferencesWrapper(Context aContext) {
		context = aContext;
		prefs = aContext.getSharedPreferences(Constants.APP_SET_PREF, Activity.MODE_PRIVATE);
	}

	public final static HashMap<String, String> STRING_PREFS = new HashMap<String, String>() {
		private static final long serialVersionUID = 1L;
		{
//			put("nettype", String.valueOf(NetWorks.NETWORK_WIFI));
		}
	};

	private final static HashMap<String, Boolean> BOOLEAN_PREFS = new HashMap<String, Boolean>() {
		private static final long serialVersionUID = 1L;
	};

	private final static HashMap<String, Float> FLOAT_PREFS = new HashMap<String, Float>() {
		private static final long serialVersionUID = 1L;
	};

	/**
	 * Enter in edit mode To use for bulk modifications
	 */
	public void startEditing() {
		sharedEditor = prefs.edit();
	}

	/**
	 * Leave edit mode
	 */
	public void endEditing() {
		if (sharedEditor != null) {
			sharedEditor.commit();
			sharedEditor = null;
		}
	}

	// Public setters
	/**
	 * Set a preference string value
	 * 
	 * @param key
	 *            the preference key to set
	 * @param value
	 *            the value for this key
	 */
	public void setPreferenceStringValue(String key, String value) {
		if (sharedEditor == null) {
			Editor editor = prefs.edit();
			editor.putString(key, value);
			editor.commit();
		} else {
			sharedEditor.putString(key, value);
		}
	}

	/**
	 * Set a preference boolean value
	 * 
	 * @param key
	 *            the preference key to set
	 * @param value
	 *            the value for this key
	 */
	public void setPreferenceBooleanValue(String key, boolean value) {
		if (sharedEditor == null) {
			Editor editor = prefs.edit();
			editor.putBoolean(key, value);
			editor.commit();
		} else {
			sharedEditor.putBoolean(key, value);
		}
	}

	/**
	 * Set a preference float value
	 * 
	 * @param key
	 *            the preference key to set
	 * @param value
	 *            the value for this key
	 */
	public void setPreferenceFloatValue(String key, float value) {
		if (sharedEditor == null) {
			Editor editor = prefs.edit();
			editor.putFloat(key, value);
			editor.commit();
		} else {
			sharedEditor.putFloat(key, value);
		}
	}

	// Private static getters
	// For string
	private static String gPrefStringValue(SharedPreferences aPrefs, String key) {
		if (aPrefs == null) {
			return STRING_PREFS.get(key);
		}
		if (STRING_PREFS.containsKey(key)) {
			return aPrefs.getString(key, STRING_PREFS.get(key));
		}
		return aPrefs.getString(key, (String) null);
	}

	// For boolean
	private static Boolean gPrefBooleanValue(SharedPreferences aPrefs,
			String key) {
		if (aPrefs == null) {
			return BOOLEAN_PREFS.get(key);
		}
		if (BOOLEAN_PREFS.containsKey(key)) {
			return aPrefs.getBoolean(key, BOOLEAN_PREFS.get(key));
		}
		if (aPrefs.contains(key)) {
			return aPrefs.getBoolean(key, false);
		}
		return null;
	}

	// For float
	private static Float gPrefFloatValue(SharedPreferences aPrefs, String key) {
		if (aPrefs == null) {
			return FLOAT_PREFS.get(key);
		}
		if (FLOAT_PREFS.containsKey(key)) {
			return aPrefs.getFloat(key, FLOAT_PREFS.get(key));
		}
		if (aPrefs.contains(key)) {
			return aPrefs.getFloat(key, 0.0f);
		}
		return null;
	}
	
	/**
	 * Get string preference value
	 * @param key the key preference to retrieve
	 * @return the value
	 */
	public String getPreferenceStringValue(String key) {
		return gPrefStringValue(prefs, key);
	}
	
	/**
	 * Get boolean preference value
	 * @param key the key preference to retrieve
	 * @return the value
	 */
	public Boolean getPreferenceBooleanValue(String key) {
		return gPrefBooleanValue(prefs, key);
	}
	
	/**
	 * Get float preference value
	 * @param key the key preference to retrieve
	 * @return the value
	 */
	public Float getPreferenceFloatValue(String key) {
		return gPrefFloatValue(prefs, key);
	}
	
	/**
	 * Get integer preference value
	 * @param key the key preference to retrieve
	 * @return the value
	 */
	public Integer getPreferenceIntegerValue(String key) {
		try {
			return Integer.parseInt(getPreferenceStringValue(key));
		}catch(NumberFormatException e) {
			LogUtil.d(TAG, "Invalid "+key+" format : expect a int");
		}
		String val = STRING_PREFS.get(key);
		if(val != null) {
		    return Integer.parseInt(val);
		}
		return null;
	}

	/**
     * Retrieve the context used for this preference wrapper
     * 
     * @return an android context
     */
	public Context getContext() {
		return context;
	}
}
